home *** CD-ROM | disk | FTP | other *** search
/ Aminet 33 / Aminet 33 - October 1999.iso / Aminet / dev / cross / ava-0.2.5.lha / ava-0.2.5 / src / Segment.h < prev    next >
Encoding:
C/C++ Source or Header  |  1999-03-23  |  4.7 KB  |  152 lines

  1. /* Segment.h, Uros Platise Dec 1998 */
  2.  
  3. #ifndef __Segment
  4. #define __Segment
  5.  
  6. #include <vector>
  7. #include "Global.h"
  8.  
  9. #define SEG_ROOT    0
  10.  
  11. #define SEG_NOTPLACED    -1
  12. #define SEG_UNKNOWNSIZE    -1
  13.  
  14. #define SEG_MAXNAMELEN    32
  15. #define SEG_LABELLEN    256
  16. #define MAX_SEGMENTS    512
  17.  
  18. /* All symbols that are defined as extern and have no prototype in
  19.    current source, should point to segment 0 due to incRef func. */
  20. #define SEGNUMBER_UNDEFINED 0
  21.  
  22. struct TSegmentRec{  
  23.   enum TFlags  {None=0,Removable=1,Abstract=2};
  24.   enum TStatus {Ok=0,OutofRange=1,Collision=2,Removed=4};
  25.   enum TMirror {NoMirror=0, NoCompression, RLE1};
  26.  
  27.   char name [SEG_MAXNAMELEN];           /* original name of the segment */
  28.   int  mirrorSegNo;             /* mirror segment */
  29.   
  30.   char label [SEG_LABELLEN];            /* label used for PC */
  31.   vector< TSegmentRec* > segRef;        /* childs */
  32.   TSegmentRec* parent;            /* parent */  
  33.   long size;                            /* maximum segment size */
  34.   long PC;                              /* running counter and tmp size */
  35.   long sumSize;                         /* size of this and all sub segs. */
  36.   long abs;                             /* when placed, this value is set.*/
  37.   TFlags flags;                         /* segment attributes */
  38.   TStatus status;            /* segment status */
  39.   TMirror mirrorType;            /* mirror segments may be compressed */
  40.   int refCount;                /* number of used labels within
  41.                           segment */
  42.   int align;                /* alignment (default to one byte) */
  43.                         
  44.   /* note: addr and size are assigned -1 value to inform linker
  45.      that these two values are to be calculated! */  
  46.   TSegmentRec(){clear();}
  47.       
  48.   void clear(){
  49.     parent=NULL; flags=None; status=Ok;
  50.     size=SEG_UNKNOWNSIZE; abs=SEG_NOTPLACED; mirrorType=NoMirror;    
  51.     PC=sumSize=0;
  52.     refCount=0;
  53.     align=1;    
  54.     name[0]=label[0]=0;     
  55.   }
  56.    
  57.   /* sort refs by ABSOLUTE number */
  58.   void sortRefs();
  59.   inline long start(){return abs+PC;}
  60.   inline long end(){return abs+sumSize;}
  61. };
  62.   
  63. class TSegTable{
  64. private:
  65.   int CRef;
  66.   struct TSegTableRec{
  67.     int newNo;        /* new segment number */
  68.     long rel;        /* relative offset per file per segment */
  69.     TSegTableRec():newNo(0),rel(0){}
  70.   };  
  71. public:
  72.   TSegTableRec seg[MAX_SEGMENTS];
  73.   TSegTable(){};
  74.   ~TSegTable(){};
  75.   friend TPt<TSegTable>;
  76. };
  77. typedef TPt<TSegTable> PSegTable;
  78.   
  79. struct TSSRec{
  80.   TSegmentRec* ref;
  81.   long start,size;
  82.  
  83.   TSSRec():ref(NULL),start(0){}  
  84.   TSSRec(long _size, TSegmentRec* _ref):ref(_ref),size(_size){}
  85.   TSSRec(long _start, long _end):start(_start){size=_end-start;}
  86.  
  87.   inline long end(){return start+size;}
  88.   inline bool operator<(const TSSRec& ssrec){return size<ssrec.size;}
  89. };    
  90.   
  91. class TSegment{
  92. private:
  93.   typedef vector< TSegmentRec* >::const_iterator TsegRecCI;
  94.   typedef vector< TSegmentRec* >::iterator TsegRecI;
  95.  
  96.   TSegmentRec segRec [MAX_SEGMENTS];  /* segment entries */
  97.   int segUsageCnt;                    /* segment usage counter */
  98.   TSegmentRec* cseg;              /* current segment in use */
  99.   bool fitted;
  100.   bool errorActive;
  101.   
  102.   void close();                  /* close current segment */
  103.   bool set(const char* name);         /* set new segment (ret: true if new)*/
  104.   void setPrevious();              /* trace back */
  105.   void update(const TSegmentRec& tmpSeg);
  106.   void checkOBSOLETE(const TSegmentRec& segTemplate);
  107.   long parseValue();
  108.   void parseSegment(bool mirror=false);
  109.   void removeUnused();
  110.   void findSum(TSegmentRec* segp, int level=0);    
  111.                                       /* stores info in .sumSize record */
  112.   void CalcMirrors();              /* calculate mirror sizes */
  113.   void fit(TSegmentRec* segp);
  114.   void saveSegmentTree(TSegmentRec* p);
  115.   void ReportSegmentTree(TSegmentRec* p, int level);
  116.   void ReportOverall(TSegmentRec* p);
  117. public:
  118.   TSegment();
  119.   ~TSegment(){}
  120.   
  121.   bool parse();                /* return true, if sth was done */  
  122.   void fitter();
  123.   void adjustSegments();
  124.   void saveSegments();
  125.   void loadSegments(int segNo);
  126.   void Report();
  127.   
  128.   char* getPC(char* PC_str);        /* returns pointer to PC_str */
  129.   bool isEnabled(int segNo);        /* ret: true if segment is enabled */
  130.   bool isAbstract(int segNo);
  131.     
  132.   void incPC(long relative);  
  133.   int getSegNo(){return cseg-segRec;}    /* returns current segment number */
  134.   void incRef(int segNo, int rel=1){
  135.     if (segNo==SEGNUMBER_UNDEFINED){return;}
  136.     assert(segNo<segUsageCnt && segNo>=0);segRec[segNo].refCount+=rel;
  137.     assert(segRec[segNo].refCount>=0);
  138.   }
  139.       
  140.   char* TellBaseSegment(int segNo); /* returns base segment; imm. after root */
  141.   int TellNoSegments();            
  142.   long  TellSize(int segNo){return segRec[segNo].PC;}
  143.   long  TellAbsolute(int segNo){return segRec[segNo].abs;}
  144.   char* TellSegmentName(int segNo);
  145.   int   TellMirror(int segNo);
  146.   int   TellAlign(int segNo);
  147. };
  148. extern TSegment segment;
  149.  
  150. #endif
  151.  
  152.